﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using M3G;
using SlimMath;
using System.IO;


namespace MyTestProject {
    public class TestSampleQuad {

        /**
         * Squad補間、Constantリピートのテスト
         * */
        [Fact]
        public void TestSplineConstant () {
            var kseq = new KeyframeSequence (4, 4, KeyframeSequence.Spline);
            kseq.RepeatMode = KeyframeSequence.Constant;
            kseq.Duration = 5;

            var q1 = Quaternion.RotationAxis (new Vector3 (0, 1, 0), (float)Math.PI * 0 / 2.0f);
            var q2 = Quaternion.RotationAxis (new Vector3 (0, 1, 0), (float)Math.PI * 1 / 2.0f);
            var q3 = Quaternion.RotationAxis (new Vector3 (0, 1, 0), (float)Math.PI * 2 / 2.0f);
            var q4 = Quaternion.RotationAxis (new Vector3 (0, 1, 0), (float)Math.PI * 3 / 2.0f);

            //            index, time, values
            kseq.SetKeyframe (0, 1, q1.ToArray ());
            kseq.SetKeyframe (1, 2, q2.ToArray ());
            kseq.SetKeyframe (2, 3, q3.ToArray ());
            kseq.SetKeyframe (3, 4, q4.ToArray ());

            using (StreamWriter writer = new StreamWriter ("TestSampleSquad-001.txt")) {
                for (var i = 0; i < 100; i++) {
                    var x = i / 100.0f * 10;
                    var value = new float[4];
                    kseq.Sample (x, 0, value);
                    writer.WriteLine ("" + x.ToString ("#0.00")
                                      + " ,    " + value[0].ToString ("#0.000") + " , " + value[1].ToString ("#0.000")
                                      + " ,    " + value[2].ToString ("#0.000") + " , " + value[3].ToString ("#0.000"));
                }
            }
        }


        /**
         * Squad補間、Loopリピートのテスト
         * */
        [Fact]
        public void TestSplineLoop () {
            var kseq = new KeyframeSequence (4, 4, KeyframeSequence.Spline);
            kseq.RepeatMode = KeyframeSequence.Loop;
            kseq.Duration = 5;

            var q1 = Quaternion.RotationAxis (new Vector3 (0, 1, 0), (float)Math.PI * 0 / 2.0f);
            var q2 = Quaternion.RotationAxis (new Vector3 (0, 1, 0), (float)Math.PI * 1 / 2.0f);
            var q3 = Quaternion.RotationAxis (new Vector3 (0, 1, 0), (float)Math.PI * 2 / 2.0f);
            var q4 = Quaternion.RotationAxis (new Vector3 (0, 1, 0), (float)Math.PI * 3 / 2.0f);

            //            index, time, values
            kseq.SetKeyframe (0, 1, q1.ToArray ());
            kseq.SetKeyframe (1, 2, q2.ToArray ());
            kseq.SetKeyframe (2, 3, q3.ToArray ());
            kseq.SetKeyframe (3, 4, q4.ToArray ());

            using (StreamWriter writer = new StreamWriter ("TestSampleSquad-002.txt")) {
                for (var i = 0; i < 100; i++) {
                    var x = i / 100.0f * 10;
                    var value = new float[4];
                    kseq.Sample (x, 0, value);
                    writer.WriteLine ("" + x.ToString ("#0.00")
                                      + " ,    " + value[0].ToString ("#0.000") + " , " + value[1].ToString ("#0.000")
                                      + " ,    " + value[2].ToString ("#0.000") + " , " + value[3].ToString ("#0.000"));
                }
            }
        }

    }
}
